<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="showSearch">
        <!-- 搜索 -->
        <el-input v-model="query.formName" clearable prefix-icon="el-icon-search" placeholder="表单名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation />
      </div>

      <el-row :gutter="10" class="mb8 uifix-crud-opts">
        <el-col :span="1.5">
          <el-button
            check-per="['flowable:form:add']"
            type="primary"
            icon="el-icon-plus"
            @click="handleAdd"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            check-per="['flowable:form:edit']"
            type="primary"
            icon="el-icon-edit"
            :disabled="crud.selections.length !== 1"
            @click="handleUpdate(crud.selections)"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            check-per="['flowable:form:del']"
            type="danger"
            icon="el-icon-delete"
            class="uifix-square-btn"
            :loading="crud.delAllLoading"
            :disabled="crud.selections.length === 0"
            @click="handleDelete(crud.selections)"
          />
        </el-col>
        <el-col :span="1.5">
          <el-button
            check-per="['flowable:form:export']"
            type="primary"
            icon="el-icon-upload2"
            plain
            @click="crud.doExport"
          >导出</el-button>
        </el-col>
      </el-row>

      <el-table ref="table" v-loading="crud.loading" stripe height="calc(100vh - 320px)" :data="crud.data" style="width: 100%;margin-top: 10px;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="formId" label="表单ID" width="80" />
        <el-table-column prop="formName" label="表单名称" />
        <el-table-column prop="remark" label="备注" />
        <!--        <el-table-column prop="formContent" label="表单内容" />-->
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column v-if="checkPer(['flowable:form:edit','flowable:form:del'])" label="操作" width="200px" align="center">
          <template slot-scope="scope">
            <div class="uifix-ud-operation-btns ">
              <el-button
                size="mini"
                type="text"
                @click="handleDetail(scope.row)"
              ><svg-icon icon-class="detail" /> 详情</el-button>
              <el-button
                size="mini"
                type="text"
                check-per="['flowable:form:edit']"
                @click="handleUpdate(scope.row)"
              ><svg-icon icon-class="icon-edit" /> 编辑</el-button>
              <el-button
                size="mini"
                class="uifix-table-action-error-btn"
                type="text"
                icon="el-icon-delete"
                check-per="['flowable:form:del']"
                @click="handleDelete(scope.row)"
              >删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>

    <!-- 添加或修改流程表单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="表单名称" prop="formName">
          <el-input v-model="form.formName" placeholder="请输入表单名称" />
        </el-form-item>
        <el-form-item label="表单内容">
          <editor v-model="form.formContent" :min-height="192" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--表单配置详情-->
    <el-dialog :title="formTitle" :visible.sync="formConfOpen" width="700px" append-to-body>
      <div class="test-form">
        <parser :key="new Date().getTime()" :form-conf="formConf" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import crudForm from '@/api/flowable/form'
import { addForm, editForm, exportForm } from '@/api/flowable/form'
import CRUD, { presenter, header, form, crud } from '@brotherCrud/crud'
import Editor from '@/components/Editor'
import Parser from '@/components/parser/Parser'
import pagination from '@brotherCrud/Pagination'
import rrOperation from '@brotherCrud/RR.operation'
import { downloadFile } from '@/utils'

const defaultForm = { formId: null, formName: null, formContent: null, createTime: null, updateTime: null, createBy: null, updateBy: null, remark: null }
export default {
  name: 'Form',
  components: { pagination, Parser, Editor, rrOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '流程表单',
      url: 'api/flowable/form',
      idField: 'formId',
      sort: 'formId,desc',
      crudMethod: { ...crudForm }
    })
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 流程表单表格数据
      formList: [],
      // 弹出层标题
      title: '',
      formConf: {}, // 默认表单数据
      formConfOpen: false,
      formTitle: '',
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},

      rules: {
        formId: [
          { required: true, message: '表单主键不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'formName', display_name: '表单名称' }
      ]
    }
  },
  created() {
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        formId: null,
        formName: null,
        formContent: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        remark: null
      }
      this.resetForm('form')
    },

    /** 重置按钮操作 */
    // resetQuery() {
    //   this.resetForm('queryForm')
    //   this.handleQuery()
    // },

    /** 表单配置信息 */
    handleDetail(row) {
      this.formConfOpen = true
      this.formTitle = '流程表单配置详细'
      this.formConf = JSON.parse(row.formContent)
      // console.log(this.formConf)
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加流程表单";
      this.$router.push({ path: '/tool/build/index', query: { formId: null }})
    },
    /** 修改按钮操作 */
    handleUpdate(data) {
      var formId = {}
      if (data.length !== undefined) { formId = data.map(item => item.formId) } else {
        formId = data.formId
      }
      // console.log(formId)
      this.$router.push({ path: '/tool/build/index', query: { formId: formId }})
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.formId != null) {
            editForm(this.form).then(response => {
              this.$message.success('修改成功')
              this.open = false
            })
          } else {
            addForm(this.form).then(response => {
              this.$message.success('新增成功')
              this.open = false
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(datas) {
      let formIds = []
      if (datas.length !== undefined) { formIds = datas.map(item => item.formId) } else {
        formIds.push(datas.formId)
      }
      this.$confirm(`是否确认删除流程表单编号为${formIds}的数据项?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.crud.delAllLoading = true
        this.crud.doDelete(datas)
        this.crud.delAllLoading = false
      }).catch((error) => {
        console.log(error)
      })
    },
    /** 导出按钮操作 此方法未使用，使用了crud.doExport*/
    handleExport(datas) {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有流程表单数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportForm(queryParams)
      }).then(response => {
        downloadFile(response, crud.title + '数据', 'xlsx')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>

